package com.dps.omc.monitor;

import com.dps.omc.model.Agent;
import com.dps.omc.service.IAgentService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by lip on 2016-05-30.
 */
@Component
public class AgentMonitor implements ApplicationListener<ContextRefreshedEvent> {

    private final static Logger logger = LoggerFactory.getLogger(AgentMonitor.class);

    private Map<String,AgentState> map = new HashMap<String, AgentState>();

    @Autowired
    private IAgentService agentService;

    private void loadMap(){
        logger.info("=========初始化代理监控开始========");
        List<Agent> agents = agentService.findAll();
        for(Agent agent:agents)
        {
            String id = agent.getId();
            String ip = agent.getIp();
            String name = agent.getName();
            map.put(agent.getId(),new AgentState(id,ip,name));
        }
        logger.info("=========初始化代理监控完毕========");
    }

    public Map<String, AgentState> getMap() {
        return map;
    }

    @Override
    public void onApplicationEvent(ContextRefreshedEvent contextRefreshedEvent) {
        loadMap();
    }
}
